package com.pds.web.testOrganization.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.testOrganization.domain.PdsTestOrganizationHonor;
import com.pds.web.testOrganization.mapper.PdsTestOrganizationHonorMapper;
import com.pds.web.testOrganization.service.IPdsTestOrganizationHonorService;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationInfo;
import com.pds.web.testOrganizationInfo.mapper.PdsTestOrganizationInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("pdsTestOrganizationHonorService")
public class PdsTestOrganizationHonorServiceImpl extends ServiceImpl<PdsTestOrganizationHonorMapper, PdsTestOrganizationHonor> implements IPdsTestOrganizationHonorService
{

    @Resource
    private PdsTestOrganizationHonorMapper pdsTestOrganizationHonorMapper;

    @Resource
    private PdsTestOrganizationInfoMapper pdsTestOrganizationInfoMapper;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public PdsTestOrganizationHonor addHonor(PdsTestOrganizationHonor pdsTestOrganizationHonor) {

//        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
//        PdsTestOrganizationInfo pdsTestOrganizationInfo = pdsTestOrganizationInfoMapper.selectByUserId(userId);
        Date createTime = DateUtils.parseDate(DateUtils.getTime());
        pdsTestOrganizationHonor.setCreateTime(createTime);
        pdsTestOrganizationHonor.setStatus("1");
//        pdsTestOrganizationHonor.setUserId(userId);
//        pdsTestOrganizationHonor.setOrganizationId(pdsTestOrganizationInfo.getId());
        pdsTestOrganizationHonorMapper.insertHonor(pdsTestOrganizationHonor);
        return pdsTestOrganizationHonor;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean deleteHonor(Long id) {
        return pdsTestOrganizationHonorMapper.deleteHonor(id) > 0;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean updateHonor(PdsTestOrganizationHonor pdsTestOrganizationHonor) {
        Date updateTime = DateUtils.parseDate(DateUtils.getTime());
        pdsTestOrganizationHonor.setUpdateTime(updateTime);
        return pdsTestOrganizationHonorMapper.updateHonor(pdsTestOrganizationHonor) > 0;
    }

    @Override
    public List<PdsTestOrganizationHonor> selectHonorlist(PdsTestOrganizationHonor pdsTestOrganizationHonor) {

        return pdsTestOrganizationHonorMapper.selectHonorlist(pdsTestOrganizationHonor);
    }
}
